using B.S.XZYData.Api.Read.Application.Command.DefectiveSummary;
using B.S.XZYData.Api.Read.DTO;
using B.S.XZYData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Read.Controllers
{
    /// <summary>
    /// 不良品汇总控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class DefectiveSummaryController : ControllerBase
    {
        private readonly IMediator _mediator;

        public DefectiveSummaryController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取不良品汇总分页数据
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页大小</param>
        /// <param name="workOrderCode">工单编号</param>
        /// <param name="productCode">产品编号</param>
        /// <param name="productName">产品名称</param>
        /// <param name="processCode">工序编号</param>
        /// <param name="processName">工序名称</param>
        /// <param name="producer">生产人员</param>
        /// <param name="productionStartTimeBegin">生产开始时间-开始</param>
        /// <param name="productionStartTimeEnd">生产开始时间-结束</param>
        /// <param name="productionEndTimeBegin">生产结束时间-开始</param>
        /// <param name="productionEndTimeEnd">生产结束时间-结束</param>
        /// <param name="defectiveItem">不良品项</param>
        /// <param name="processPlanQuantityMin">工序计划数-最小值</param>
        /// <param name="processPlanQuantityMax">工序计划数-最大值</param>
        /// <param name="reportQuantityMin">报工数-最小值</param>
        /// <param name="reportQuantityMax">报工数-最大值</param>
        /// <param name="qualifiedQuantityMin">良品数-最小值</param>
        /// <param name="qualifiedQuantityMax">良品数-最大值</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPageing<DefectiveSummaryDto>> GetDefectiveSummary(
            [FromQuery] int index = 1,
            [FromQuery] int size = 30,
            [FromQuery] string? workOrderCode = null,
            [FromQuery] string? productCode = null,
            [FromQuery] string? productName = null,
            [FromQuery] string? processCode = null,
            [FromQuery] string? processName = null,
            [FromQuery] string? producer = null,
            [FromQuery] DateTime? productionStartTimeBegin = null,
            [FromQuery] DateTime? productionStartTimeEnd = null,
            [FromQuery] DateTime? productionEndTimeBegin = null,
            [FromQuery] DateTime? productionEndTimeEnd = null,
            [FromQuery] string? defectiveItem = null,
            [FromQuery] int? processPlanQuantityMin = null,
            [FromQuery] int? processPlanQuantityMax = null,
            [FromQuery] int? reportQuantityMin = null,
            [FromQuery] int? reportQuantityMax = null,
            [FromQuery] int? qualifiedQuantityMin = null,
            [FromQuery] int? qualifiedQuantityMax = null,
            [FromQuery] string? sortField = null,
            [FromQuery] string? sortOrder = null)
        {
            var command = new GetDefectiveSummaryCommand
            {
                Index = index,
                Size = size,
                WorkOrderCode = workOrderCode,
                ProductCode = productCode,
                ProductName = productName,
                ProcessCode = processCode,
                ProcessName = processName,
                Producer = producer,
                ProductionStartTimeBegin = productionStartTimeBegin,
                ProductionStartTimeEnd = productionStartTimeEnd,
                ProductionEndTimeBegin = productionEndTimeBegin,
                ProductionEndTimeEnd = productionEndTimeEnd,
                DefectiveItem = defectiveItem,
                ProcessPlanQuantityMin = processPlanQuantityMin,
                ProcessPlanQuantityMax = processPlanQuantityMax,
                ReportQuantityMin = reportQuantityMin,
                ReportQuantityMax = reportQuantityMax,
                QualifiedQuantityMin = qualifiedQuantityMin,
                QualifiedQuantityMax = qualifiedQuantityMax,
                SortField = sortField,
                SortOrder = sortOrder
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 导出不良品汇总
        /// </summary>
        /// <param name="workOrderCode">工单编号（模糊查询）</param>
        /// <param name="productCode">产品编号（模糊查询）</param>
        /// <param name="productName">产品名称（模糊查询）</param>
        /// <param name="processCode">工序编号（模糊查询）</param>
        /// <param name="processName">工序名称（模糊查询）</param>
        /// <param name="producer">生产人员（模糊查询）</param>
        /// <param name="productionStartTimeBegin">生产开始时间-开始</param>
        /// <param name="productionStartTimeEnd">生产开始时间-结束</param>
        /// <param name="productionEndTimeBegin">生产结束时间-开始</param>
        /// <param name="productionEndTimeEnd">生产结束时间-结束</param>
        /// <param name="reportQuantityMin">报工数量-最小值</param>
        /// <param name="reportQuantityMax">报工数量-最大值</param>
        /// <param name="qualifiedQuantityMin">合格数量-最小值</param>
        /// <param name="qualifiedQuantityMax">合格数量-最大值</param>
        /// <returns>Excel文件</returns>
        [HttpGet]
        public async Task<IActionResult> ExportDefectiveSummary(
            [FromQuery] string? workOrderCode = null,
            [FromQuery] string? productCode = null,
            [FromQuery] string? productName = null,
            [FromQuery] string? processCode = null,
            [FromQuery] string? processName = null,
            [FromQuery] string? producer = null,
            [FromQuery] DateTime? productionStartTimeBegin = null,
            [FromQuery] DateTime? productionStartTimeEnd = null,
            [FromQuery] DateTime? productionEndTimeBegin = null,
            [FromQuery] DateTime? productionEndTimeEnd = null,
            [FromQuery] int? reportQuantityMin = null,
            [FromQuery] int? reportQuantityMax = null,
            [FromQuery] int? qualifiedQuantityMin = null,
            [FromQuery] int? qualifiedQuantityMax = null)
        {
            var command = new ExportDefectiveSummaryCommand
            {
                WorkOrderCode = workOrderCode,
                ProductCode = productCode,
                ProductName = productName,
                ProcessCode = processCode,
                ProcessName = processName,
                Producer = producer,
                ProductionStartTimeBegin = productionStartTimeBegin,
                ProductionStartTimeEnd = productionStartTimeEnd,
                ProductionEndTimeBegin = productionEndTimeBegin,
                ProductionEndTimeEnd = productionEndTimeEnd,
                ReportQuantityMin = reportQuantityMin,
                ReportQuantityMax = reportQuantityMax,
                QualifiedQuantityMin = qualifiedQuantityMin,
                QualifiedQuantityMax = qualifiedQuantityMax
            };

            var result = await _mediator.Send(command);

            if (result.Code == ApiEnum.Success && result.Data != null)
            {
                var fileName = $"不良品汇总_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
                var encodedFileName = Uri.EscapeDataString(fileName);
                return File(result.Data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
            }

            return BadRequest(result.Msg);
        }
    }
} 